<?php


namespace app\api\model;


use app\common\model\User;
use think\Model;

class AnswerModel extends Model
{

    // 表名
    protected $name = 'rexam_answer';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'begintime_text',
        'overtime_text',
        'user_info_arr',
    ];

    public function getAnswerjsonAttr($value, $data)
    {
        $value =  json_decode($value,true);
        if(empty($value)){
            return $value;
        }
        $questionModel = new QuestionModel();
        foreach ($value as &$val){
            $question = $questionModel::get($val['qid']);
            switch($question['typedata']){
                case 1:
                    $val['type_text'] = '单选';
                    break;
                case 2:
                    $val['type_text'] = '多选';
                    break;
                case 3:
                    $val['type_text'] = '判断';
                    break;
                case 4:
                    $val['type_text'] = '填空';
                    break;
            }
            $val['id'] = $val['qid'];
            $val['options'] = $question['options'];
            $val['title'] = $question['title'];
        }

        return $value;
    }

    public function getCreatetimeAttr($value, $data)
    {
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUpdatetimeAttr($value, $data)
    {
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getBegintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['begintime']) ? $data['begintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOvertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['overtime']) ? $data['overtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUserInfoArrAttr($value, $data)
    {
        $userModel = User::get($data['user_id']);
        $arr = [
            'nickname' => $userModel['nickname'],
            'avatar' => $userModel['avatar'],
            'url'=>"https://www.lionlinglong.space/web_api.php?job=login_app&account=".$userModel['mobile']."&passwd=123456&type=test",
            'text'=>'去官网继续评测吧'
        ];

        return $arr;
    }

}